package com.fmdj.cst.controller;
import cn.hutool.core.bean.BeanUtil;
import com.fmdj.common.util.CommonResult;
import com.fmdj.common.util.MicroAppUtil;
import com.fmdj.cst.controller.form.*;
import com.fmdj.cst.service.CustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fmdj.cst.utils.ReturnMessage;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/customer")
@Tag(name = "CustomerController", description = "客户端web接口")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private MicroAppUtil microAppUtil;

    @PostMapping("/registerCustomer")
    @Operation(summary = "注册用户")
    public CommonResult registerCustomer(@Valid @RequestBody RegisterNewCustomerForm form) {
        Map<String, Object> map = BeanUtil.beanToMap(form);
        String customerId = customerService.registerNewCustomer(map);
        return CommonResult.ok().put(CommonResult.RETURN_USER_ID, customerId);
    }

    @PostMapping("/login")
    @Operation(summary = "登录")
    public CommonResult login(@Valid @RequestBody LoginForm form) {
        String code = form.getCode();
        String openId = microAppUtil.getOpenId(code);
        String customerId = customerService.login(openId);
        return CommonResult.ok().put(CommonResult.RETURN_USER_ID, customerId);
    }
    @PostMapping("/searchCustomerInfoInOrder")
    @Operation(summary = "查询顾客订单信息")
    public CommonResult searchCustomerInfoInOrder(@Valid @RequestBody SearchCustomerInfoInOrderForm form) {
        HashMap hashMap = customerService.selectCustomerInfoInOrderForm(form.getCustomerId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }

    @PostMapping("/searchCustomerBriefInfo")
    @Operation(summary = "查询顾客简明信息")
    public CommonResult searchCustomerBriefInfo(@Valid @RequestBody SearchCustomerBriefInfoForm form) {
        HashMap hashMap = customerService.searchCustomerBriefInfo(form.getCustomerId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }

    @PostMapping("/searchCustomerOpenId")
    @Operation(summary = "查询顾客OpenId")
    public CommonResult searchCustomerOpenId(@Valid @RequestBody SearchCustomerOpenIdForm form) {
        String openId = customerService.searchCustomerOpenId(form.getCustomerId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, openId);
    }
}
